package service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import pojo.Tb_user;
import dao.BaseDao;
@Service("UserService")
public class UserServiceImpl implements UserService{
	@Resource
	BaseDao<Tb_user> bd;
	public void insert(Tb_user user) {
		bd.insert(user);
		
	}
	
	public List<Tb_user> find(Tb_user user) {
		
		List<Object>param=new ArrayList<Object>();
		if(user.getName()!=null&&!user.getName().equals(""))
			param.add(user.getName());
		if(user.getPassword()!=null&&!user.getPassword().equals(""))
			param.add(user.getPassword());
		return bd.find(" from Tb_user where name=? and password=?", param);
	}

	public void update(Tb_user user) {
		List<Object> param = new ArrayList<Object>();
		StringBuffer hql=new StringBuffer("update Tb_user set");
		if(user.getName()!=null||!user.getName().equals("")){
			hql.append(" name=?");
			param.add(user.getName());
		}
		if(user.getPassword()!=null||!user.getPassword().equals("")){
			hql.append(",password=?");
			param.add(user.getPassword());
		}
		if(user.getRole()!=null||!user.getRole().equals("")){
			hql.append(",role=?");
			param.add(user.getRole());
		}
		String where="where id="+user.getId();
		hql.append(where);
		String sql=hql.toString();
		bd.update(sql, param);
		
	}

	public void delete(int id) {
		bd.delete(Tb_user.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Tb_user> findpage(int first) {
		// TODO Auto-generated method stub
		int a=first*10;
		int b=a+11;
		List<Object> param=new LinkedList<Object>();
		param.add(a);
		param.add(b);
		List<Tb_user> userlist=
		bd.findPage("from Tb_user where id>? and id<?", param).list();
		return userlist;
	}

	public List<Tb_user> findall() {
		// TODO Auto-generated method stub
		List<Tb_user>list=new ArrayList<Tb_user>();
		list=bd.find("from Tb_user", null);
		return list;
	}

	public Tb_user findbyid(int id) {
		Tb_user u=bd.findById(Tb_user.class,id );
		return u;
	}

}
